var MsgProto = require('./msg_pb');


//  var message2 = basepb.requestModel.deserializeBinary(bytes);
//  console.log(message2);

// 1 引入模块
const net = require('net');

let client = new net.Socket();
// 3 链接
client.connect(8899, 'localhost');

// client.setEncoding('utf8');

// import encoderClass from '../socket/Varint32/ProtoVarint32Encoder'

// let encoder = new encoderClass();
// let out=encoder.encode(bytes)
var ByteBuffer = require("bytebuffer");
var sbuf = new ByteBuffer();
// for (let i = 0; i < 100; i++) {
let message = new MsgProto.Msg();
message.setBody("hello");
message.setType(MsgProto.Msg.Type.MESSAGE_FRIEND);
let bytes = message.serializeBinary();
console.log(message)


let buffer = sbuf.int32(bytes.byteLength)//增加了长度
    .byteArray(bytes, bytes.byteLength)
    .pack();  //打包
// console.log("ByteBuffer编码后的长度：" + buffer.byteLength);
// console.log("ByteBuffer编码后：" + buffer);


for (let index = 0; index < 100; index++) {
    client.write(buffer);
}

// }





client.on('data', (bytes) => {
    var val = MsgProto.Msg().deserializeBinary(bytes);
    console.log(val);
})
client.on('error', (e) => {
    console.log(e.message);
})